package com.laomo.lib_draw_text;

import android.app.Activity;

import androidx.annotation.NonNull;

import java.util.HashMap;
import java.util.Map;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/**
 * LibDrawTextPlugin
 */
public class LibDrawTextPlugin implements FlutterPlugin,ActivityAware, MethodCallHandler {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private MethodChannel channel;
    private Activity mContext;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "lib_draw_text");
        channel.setMethodCallHandler(this);
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        switch (call.method) {
            case "drawText":
                Map<String, Object> args = (Map<String, Object>) call.arguments;
                int height = (int) args.get("height");
                int textHeight = height;
                if (args.containsKey("textHeight")) {
                    textHeight = (int) args.get("textHeight");
                }
                byte[] b = DrawTextUtil.getTextData(mContext, (String) args.get("text"), (int) args.get("width"), height, textHeight);
                Map<String, Object> map = new HashMap<>();
                map.put("data", b);
                result.success(map);
                break;
            case "setBold":
                DrawTextUtil.setBold((boolean) call.arguments);
                result.success(null);
                break;
            case "setItalic":
                DrawTextUtil.setItalic((boolean) call.arguments);
                result.success(null);
                break;
            case "setFontFilePath":
                DrawTextUtil.setFontFilePath((String) call.arguments);
                result.success(null);
                break;
            case "getPlatformVersion":
                result.success("Android " + android.os.Build.VERSION.RELEASE);
                break;
            default:
                result.notImplemented();
        }
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        mContext = binding.getActivity();
    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
    }

    @Override
    public void onDetachedFromActivity() {
    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {
    }
}
